package com.qf.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.mapper.DtsBrandMapper;
import com.qf.pojo.DtsAdmin;
import com.qf.pojo.DtsBrand;
import com.qf.pojo.DtsCategory;
import com.qf.service.AdminService;
import com.qf.service.BrandService;
import com.qf.service.CategoryService;
import com.qf.util.ResponseUtil;
import com.qf.vo.CatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/brand")
@CrossOrigin("*")
public class BrandController {


    @Autowired
    private BrandService brandService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AdminService adminService;


    @RequestMapping("/list")
    public Object list(Integer id, String name,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @RequestParam(defaultValue = "add_time") String sort,
                       @RequestParam(defaultValue = "desc") String order) {

        IPage<DtsBrand> resultPage = brandService.queryBrandPage(id, name, page, limit, sort, order);

        List<DtsBrand> list = resultPage.getRecords();

        long total = resultPage.getTotal();
        Map<String, Object> data = new HashMap<>();

        data.put("total", total);
        data.put("items", list);

        return ResponseUtil.ok(data);

    }


    @RequestMapping("/catAndAdmin")
    public Object catAndAdmin() {

        List<CatVo> lis = categoryService.selectcatVo11();
        List<CatVo> list2 = categoryService.selectCategory();
        ArrayList<CatVo> list = new ArrayList<>();
        ArrayList<CatVo> list3 = new ArrayList<>();
        for (CatVo vo:lis) {
            for (CatVo catVo:list2) {
                list3.add(catVo);
            }
            vo.setChildren(list3);
            list.add(vo);
        }
        List<DtsAdmin> list1 =adminService.selectAll();
        List<CatVo> adminList = new ArrayList<>();
        for (DtsAdmin admin: list1) {
            CatVo vo = new CatVo();
            vo.setValue(admin.getId());
            vo.setLabel(admin.getUsername());
            adminList.add(vo);
        }

        Map<String, Object> data = new HashMap<>();
        data.put("adminList",adminList);
        data.put("categoryList",list);

        return ResponseUtil.ok(data);
    }
    @RequestMapping("/create")
    public Object create(@RequestBody DtsBrand brand){

        if (brand.getName()==null){
            throw new RuntimeException("名字不可以为空！！");
        }
        DtsBrand dtsBrand = brandService.add(brand);

        return ResponseUtil.ok(brand);
    }

    @RequestMapping("/update")
    public Object update(@RequestBody DtsBrand brand){

        if (brand.getName()==null){
            throw  new RuntimeException("````");
        }
        brandService.update(brand);
        return ResponseUtil.ok();


    }
    @RequestMapping("/delete")
    public Object delete(@RequestBody DtsBrand brand){
        Integer id = brand.getId();
        if (StringUtils.isEmpty(id)){
            return ResponseUtil.badArgument();
        }
        brandService.delete(id);

        return ResponseUtil.ok();
    }
}
